package main

import (
	"gitee.com/shuokeyun/api-gateway/internal/biz"
	"gitee.com/shuokeyun/api-gateway/internal/service"
	v1 "gitee.com/shuokeyun/apis/merchant-service/api/v1"
	"gitee.com/shuokeyun/kratos/client"
	"gitee.com/shuokeyun/kratos/server"
	"github.com/fullstorydev/grpcurl"
	"github.com/go-kratos/kratos/v2/transport/http"
	"github.com/google/wire"
)

var providerSet = wire.NewSet(
	newRegisterHttpServer,
	loadDescriptorSource,
	biz.NewGrpcClientConnManage,
	biz.NewProxy,
	service.NewApiGateway,
	newUserAuthClient,
)

func newRegisterHttpServer(gateway *service.ApiGateway) server.WithAfterHTTPServerFunc {
	return func(g *http.Server) error {
		r := g.Route("/")
		r.GET("/{bff}/{service}/{method}/{version}", func(context http.Context) error {
			return gateway.Handler(context)
		})
		r.POST("/{bff}/{service}/{method}/{version}", func(context http.Context) error {
			return gateway.Handler(context)
		})
		return nil
	}
}

func loadDescriptorSource() (grpcurl.DescriptorSource, error) {
	protoset := []string{"/Users/coco/code/gocode/geekbang-graduation/api-gateway/api/v1/med_bff.protoset"}
	fileSource, err := grpcurl.DescriptorSourceFromProtoSets(protoset...)
	if err != nil {
		return nil, err
	}
	return fileSource, nil
}

func newUserAuthClient(app *server.App) (v1.UserAuthClient, error) {
	insecure, err := client.NewGrpcClientConn(app.GetDiscovery(), v1.UserAuth_ServiceDesc.ServiceName, app.GetConfigBootstrap().GetServer().GetName())
	if err != nil {
		return nil, err
	}
	return v1.NewUserAuthClient(insecure), nil
}
